package org.javabasics.io;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Created by LUOLIANG on 2016/9/21.
 * 使用字符串作为物理节点的字符输入、输出流的用法
 */
public class StringNodeTest {
    public static void main(String[] args) {
        String src = "从明天起，做一个幸福的人\n" + "吃饭，睡觉，打豆豆";
        char bbuf[] = new char[32];
        int hasRead = 0;
        try (
                StringReader sr = new StringReader(src);
        ) {
            while ((hasRead = sr.read(bbuf)) > 0) {
                System.out.println(new String(bbuf, 0, hasRead));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (
                StringWriter sw = new StringWriter()
        ) {
            sw.write("有一个程序猿\n");
            sw.write("他正在写代码");
            System.out.println("下面是sw字符串节点的内容：");
            System.out.println(sw.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
